home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows5 / vbterm.zip / SERIAL.BAS < prev    next >
BASIC Source File  |  1991-09-01  |  7KB  |  257 lines

  1. '
  2. '   FILE    SERIAL.BAS
  3. '
  4. '       This is the code to handle the interface to the windows comm API
  5. '
  6. '   Charles McGuinness [76701,11]
  7. '
  8. '
  9. '    Serial Communications Module for VB
  10. '
  11.  
  12. '
  13. '  COMM declarations
  14. '
  15. Const NOPARITY = 0
  16. Const ODDPARITY = 1
  17. Const EVENPARITY = 2
  18. Const MARKPARITY = 3
  19. Const SPACEPARITY = 4
  20.  
  21. Const ONESTOPBIT = 0
  22. Const ONE5STOPBITS = 1
  23. Const TWOSTOPBITS = 2
  24.  
  25. Const IGNORE = 0 '  Ignore signal
  26. Const INFINITE = &HFFFF  '  Infinite timeout
  27.  
  28. '  Error Flags
  29. Const CE_RXOVER = &H1    '  Receive Queue overflow
  30. Const CE_OVERRUN = &H2   '  Receive Overrun Error
  31. Const CE_RXPARITY = &H4  '  Receive Parity Error
  32. Const CE_FRAME = &H8     '  Receive Framing error
  33. Const CE_BREAK = &H10    '  Break Detected
  34. Const CE_CTSTO = &H20    '  CTS Timeout
  35. Const CE_DSRTO = &H40    '  DSR Timeout
  36. Const CE_RLSDTO = &H80   '  RLSD Timeout
  37. Const CE_TXFULL = &H100  '  TX Queue is full
  38. Const CE_PTO = &H200     '  LPTx Timeout
  39. Const CE_IOE = &H400     '  LPTx I/O Error
  40. Const CE_DNS = &H800     '  LPTx Device not selected
  41. Const CE_OOP = &H1000    '  LPTx Out-Of-Paper
  42. Const CE_MODE = &H8000   '  Requested mode unsupported
  43.  
  44. Const IE_BADID = (-1)    '  Invalid or unsupported id
  45. Const IE_OPEN = (-2)     '  Device Already Open
  46. Const IE_NOPEN = (-3)    '  Device Not Open
  47. Const IE_MEMORY = (-4)   '  Unable to allocate queues
  48. Const IE_DEFAULT = (-5)  '  Error in default parameters
  49. Const IE_HARDWARE = (-10)        '  Hardware Not Present
  50. Const IE_BYTESIZE = (-11)        '  Illegal Byte Size
  51. Const IE_BAUDRATE = (-12)        '  Unsupported BaudRate
  52.  
  53. '  Events
  54. Const EV_RXCHAR = &H1    '  Any Character received
  55. Const EV_RXFLAG = &H2    '  Received certain character
  56. Const EV_TXEMPTY = &H4   '  Transmitt Queue Empty
  57. Const EV_CTS = &H8       '  CTS changed state
  58. Const EV_DSR = &H10      '  DSR changed state
  59. Const EV_RLSD = &H20     '  RLSD changed state
  60. Const EV_BREAK = &H40    '  BREAK received
  61. Const EV_ERR = &H80      '  Line status error occurred
  62. Const EV_RING = &H100    '  Ring signal detected
  63. Const EV_PERR = &H200    '  Printer error occured
  64.  
  65. '  Escape Functions
  66. Const SETXOFF = 1        '  Simulate XOFF received
  67. Const SETXON = 2 '  Simulate XON received
  68. Const SETRTS = 3 '  Set RTS high
  69. Const CLRRTS = 4 '  Set RTS low
  70. Const SETDTR = 5 '  Set DTR high
  71. Const CLRDTR = 6 '  Set DTR low
  72. Const RESETDEV = 7       '  Reset device if possible
  73.  
  74. Const LPTx = &H80        '  Set if ID is for LPT device
  75.  
  76.  
  77. Declare Function OpenComm Lib "User" (ByVal lpComName As String, ByVal wInQueue As Integer, ByVal wOutQueue As Integer) As Integer
  78. Declare Function SetCommState Lib "User" (lpDCB As DCB) As Integer
  79. Declare Function GetCommState Lib "User" (ByVal nCid As Integer, lpDCB As DCB) As Integer
  80. Declare Function ReadComm Lib "User" (ByVal nCid As Integer, ByVal lpBuf As String, ByVal nSize As Integer) As Integer
  81. Declare Function UngetCommChar Lib "User" (ByVal nCid As Integer, ByVal cChar As Integer) As Integer
  82. Declare Function WriteComm Lib "User" (ByVal nCid As Integer, ByVal lpBuf As String, ByVal nSize As Integer) As Integer
  83. Declare Function CloseComm Lib "User" (ByVal nCid As Integer) As Integer
  84. Declare Function BuildCommDCB Lib "User" (ByVal lpDef As String, lpDCB As DCB) As Integer
  85. Declare Function TransmitCommChar Lib "User" (ByVal nCid As Integer, ByVal cChar As Integer) As Integer
  86. Declare Function SetCommEventMask Lib "User" (ByVal nCid As Integer, nEvtMask As Integer) As Long
  87. Declare Function GetCommEventMask Lib "User" (ByVal nCid As Integer, ByVal nEvtMask As Integer) As Integer
  88. Declare Function SetCommBreak Lib "User" (ByVal nCid As Integer) As Integer
  89. Declare Function ClearCommBreak Lib "User" (ByVal nCid As Integer) As Integer
  90. Declare Function FlushComm Lib "User" (ByVal nCid As Integer, ByVal nQueue As Integer) As Integer
  91. Declare Function EscapeCommFunction Lib "User" (ByVal nCid As Integer, ByVal nFunc As Integer) As Integer
  92. Declare Function GetCommError Lib "User" (ByVal nCid As Integer, lpStat As Any) As Integer
  93.  
  94. '
  95. '   Bits  for bits1 and bits2
  96. '
  97.  
  98. '   Bits1
  99. Const fbinary = &H1
  100. Const frtsdiable = &H2
  101. Const fparity = &H4
  102. Const foutxctsflow = &H8
  103. Const foutxdsrflow = &H10
  104. Const fdtrdisable = &H80
  105.  
  106. '   Bits2
  107.  
  108. Const foutx = &H1
  109. Const finx = &H2
  110. Const fpechar = &H4
  111. Const fnull = &H8
  112. Const fchevt = &H10
  113. Const fdtrflow = &H20
  114. Const frtsflow = &H40
  115.  
  116. '
  117. '   Definitions of our open port
  118. '
  119. Dim nCid        As Integer
  120. Dim PortName    As String
  121. Dim OutSize     As Integer
  122. Dim lpDCB       As DCB
  123.  
  124. Function SerialOpen (ComPort As Integer) As Integer
  125. '
  126. '    Open the serial port. Expects the com port number as the argument
  127. '    and returns either zero for success, or non-zero on error
  128. '
  129.     PortName = "COM" + Format$(ComPort, "#")
  130.     OutSize = 256
  131.     nCid = OpenComm(PortName, 2048, OutSize)
  132.     If (nCid < 0) Then
  133.         SerialOpen = nCid
  134.     Else
  135.         SerialOpen = 0
  136.     End If
  137. End Function
  138.  
  139. Function SerialClose () As Integer
  140. '
  141. '    Closes the serial port.  Zero return on OK
  142. '
  143.     x% = CloseComm(nCid)
  144.     If (x% < 0) Then
  145.         SerialClose = x%
  146.     Else
  147.         SerialClose = 0
  148.     End If
  149. End Function
  150.  
  151. Function SerialConfig (baud%, bits%, Parity$) As Integer
  152. '
  153. '    Configure the open serial port
  154. '
  155.     Dim ConfigString As String
  156.  
  157.     ConfigString = PortName + ":"
  158.  
  159.     ConfigString = ConfigString + Format$(baud%) + ","
  160.  
  161.     ConfigString = ConfigString + Left$(UCase$(Parity$), 1) + ","
  162.  
  163.     ConfigString = ConfigString + Format$(bits%, "#") + ",1"
  164.  
  165.     i% = BuildCommDCB(ConfigString, lpDCB)
  166.  
  167.     lpDCB.id = Chr$(nCid)
  168.     lpDCB.bits2 = Chr$(Asc(lpDCB.bits2) Or finx)
  169.     lpDCB.XonChar = Chr$(Asc("Q") - 64)
  170.     lpDCB.XoffChar = Chr$(Asc("S") - 64)
  171.     lpDCB.XonLim = 256
  172.     lpDCB.XoffLim = 256
  173.  
  174.     SerialConfig = SetCommState(lpDCB)
  175.  
  176. End Function
  177.  
  178. Function serialwrite (t$) As Integer
  179.  
  180.     If (SerialOutFree() < Len(t$)) Then
  181.  
  182.         '   Wait for enough space in our buffer
  183.         Do
  184.             x% = DoEvents()
  185.         Loop While SerialOutFree() < Len(t$)
  186.  
  187.     End If
  188.  
  189.     serialwrite = WriteComm(nCid, t$, Len(t$))
  190.  
  191. End Function
  192.  
  193. Function SerialRead (buf$, max%) As Integer
  194. '    Dim st As COMSTAT
  195.     Static last As Integer
  196.  
  197.  
  198.     i% = ReadComm(nCid, buf$, max%)
  199.     SerialRead = i%
  200.  
  201.     If (i% < 0) Or ((i% = 0) And (last = 0)) Then
  202.         status% = GetCommError(nCid, ByVal 0&)
  203.         SerialRead = -i%
  204.     End If
  205.  
  206.     last = i%
  207.  
  208. End Function
  209.  
  210. Sub SerialBreak (state%)
  211.     If (state%) Then
  212.         r% = SetCommBreak(nCid)
  213.     Else
  214.         r% = ClearCommBreak(nCid)
  215.     End If
  216.  
  217. End Sub
  218.  
  219. Function SerialOutFree ()
  220. '
  221. '   Returns the amount of free space in the output
  222. '   buffer (to prevent overruns, provide pacing, etc.)
  223. '
  224.     Dim st As COMSTAT
  225.  
  226.     status% = GetCommError(nCid, st)
  227.     
  228.     If (status% <> 0) Then Beep
  229.  
  230.     SerialOutFree = OutSize - st.cbOutQue
  231.  
  232. End Function
  233.  
  234. Sub serialbinary (yesno As Integer)
  235.  
  236.     Dim TempDCB As DCB
  237.  
  238.     If (yesno = 0) Then     ' Turn off Binary Mode
  239.         x% = SetCommState(lpDCB)
  240.         Exit Sub
  241.     End If
  242.  
  243.     ' Turn On Binary Mode
  244.     TempDCB = lpDCB
  245.  
  246.     TempDCB.ByteSize = Chr$(8)
  247.  
  248.     TempDCB.Parity = Chr$(NOPARITY)
  249.  
  250.     TempDCB.bits1 = Chr$(fbinary)
  251.  
  252.     TempDCB.bits2 = Chr$(0)
  253.  
  254.     x% = SetCommState(TempDCB)
  255. End Sub
  256.  
  257.